package com.mohe.dangjian.filter;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LoginInterceptor implements Interceptor {
	
	private static final long serialVersionUID = 1L;

	// 日志
	private static final Log log = LogFactory.getLog(LoginInterceptor.class);

	// 不过滤的Action方法
	private Set<String> actionSet = null;

	@Override
	public void destroy() {
		actionSet.clear();
		actionSet = null;
	}

	@Override
	public void init() {
		actionSet = new HashSet<String>();
		actionSet.add("login");
		//actionSet.add("toLogin");
		//actionSet.add("getCheck");
		//actionSet.add("register");
		//actionSet.add("checkUser");
		//actionSet.add("userLogin");
		//actionSet.add("editUser");
	}

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {

		// 请求Action和方法
		String actionName = actionInvocation.getInvocationContext().getName();
		String method = actionInvocation.getProxy().getMethod();

		// 最终跳转页面
		String jsp = "login";
		try {
			if (actionSet.contains(method)) {
				jsp = actionInvocation.invoke();
			} else {
				// 确认Session中是否存在登录用户
				Map<String, Object> session = actionInvocation.getInvocationContext().getSession();
				Long adminKey = (Long) session.get("adminKey");
				if (null != adminKey) {
					jsp = actionInvocation.invoke();
				}
			}
		} catch (Exception e) {
			log.error("__________________" + actionName, e);
			jsp = "login";
		}
		return jsp;
	}

}
